#! /bin/bash

# 更新下debian的仓库，使用国内源
mv /etc/apt/sources.list /etc/apt/sources.list.bak
cp ./sources.list /etc/apt/sources.list
apt-get update

# ----开始安装docker和docker-compose----------
# 安装必要的软件包
apt-get install -y ca-certificates   curl   gnupg  lsb-release sudo
# 设置dokcer的源
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo  "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
apt-get update
# 安装docker
apt-get install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
mkdir /etc/docker
# 使用docker的国内镜像仓库
cp ./daemon.json /etc/docker
systemctl restart docker
docker info

# 安装docker-compose
cp ./docker-compose-linux-x86-64 /usr/bin/docker-compose
chmod +x  /usr/bin/docker-compose

# ---------下载项目并打包--------------

# 安装jdk
apt install -y  openjdk-11-jdk
# 克隆项目到本机
git clone https://gitee.com/many2many/j2ee.git j2ee
# 项目复制到/opt/pet目录
cp -r j2ee/课程/petclinic /opt/pet
cd /opt/pet
# 设置可执行权限
chmod +x ./gradlew
# 编译和打包项目
./gradlew build

# ---------容器方式启动各个服务
# 创建docker 网络，将项目所有服务都置于此网络内
docker network create --subnet 172.66.0.1/16 pet_net

# 启动nacos服务容器
cd /opt/pet/nacos-docker
docker-compose -f standalone-derby.yml up -d

# 启动gateway
cd ../api-gateway
docker-compose up -d

# 启动vet service
cd ../vet-service
docker-compose up -d

# 启动customer-service
cd ../customer-service
docker-compose up -d




